<%@page import="java.util.Date"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="com.shopping.user.*" %>
<%@ page import="com.shopping.service.*" %>
<%@ page import="java.util.*" %>

<!DOCTYPE html>
<%
    request.setCharacterEncoding("utf-8");
	String user = request.getParameter("action");

	if(user != null && user.equals("user")) {
		User u = new User();
		u.setId(-1);
		u.setUsername(request.getParameter("user"));
		u.setPassword(request.getParameter("pw1"));
		u.setPhone(request.getParameter("phone"));
		u.setAddr (request.getParameter("addr"));
		System.out.println(u.getUsername());
		u.setPdate(new Date());
		
		//验证username是否存在
		boolean isExist = UserService.existUser(u.getUsername());
		
		
		UserService.save(u);
		
		out.println("恭喜注册成功");
%>   
		<br>     
		<a href="userlogin.jsp">点击登录</a>
<%		
        return;
	}
%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
  function checkdata() {
	  var username = form.user.value.trim();
	  if(username.length <= 0 || username.length > 5 ) {
		  alert("cuowu")
		  return false;
	  }
	  var pw1 = form.pw1.value.trim();
	  if(pw1.length < 6 || pw1.lenght > 10) {
		  alert("密码必须是6-10位数")
		  return false;
	  }
	  
	  var pw2 = form.pw2.value;
	  
	  //(pw2.value != pw1.value) 不能用上面的变量值
	  //(pw2.equals(pw1)) 没有equals方法
		  if(pw2.toString() != form.pw1.value) { //可以用valueOf方法返回字符串,也可以用toString方法
			  alert(pw2);						//最简单应该写成form.pw2.value != form.pw1.value;
			  alert("两次输入密码不一致");
			  return false;
		  }
	  
	  var phone = form.phone.value.trim();
	  if(phone.length != 11) {
		  alert("请输入正确的11位手机号码");
		  return false;
	  }
	  
	  var addr = form.addr.value.trim();
	  if(addr.length <= 0) {
		  alert("请输入地址");
		  return false;
	  }
  }

  
 
</script>
</head>
<body>
  <form name="form" action="register.jsp" method="post" onSubmit="return checkdata()">
      <input type="hidden" name="action" value="user">
	  <table>
	    <tr>
	      <td>
	                      用户名：&nbsp;&nbsp;&nbsp;&nbsp;<input type="text" name="user" size=30>
	      </td>
	    </tr>
	    <tr>
	      <td>
	                      密码：&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
	             <input type="password " name="pw1" size=30>
	      </td>
	    </tr>
	    <tr>
	      <td>
	                      确认密码：<input type="password " name="pw2" size=30>
	      </td>
	    </tr>
	    <tr>
	      <td>
	                      手机号：&nbsp;&nbsp;&nbsp;&nbsp;<input type="text" name="phone" size=30>
	      </td>
	    </tr>
	    <tr>
	      <td>
	                      联系地址：<textarea name="addr" cols="30" rows="10"></textarea>
	      </td>
	    </tr>
	    <tr>
	      <td colspan="2">
	         <input type="submit" name="submit" value="提交">            
	      </td>
	    </tr>
	  </table>
	</form>>
</body>
</html>